package com.bilubi.reggie.reggie.controller;

import com.bilubi.reggie.reggie.bean.Dish;
import com.bilubi.reggie.reggie.bean.DishFlavor;
import com.bilubi.reggie.reggie.bean.vo.DishVo;
import com.bilubi.reggie.reggie.common.R;
import com.bilubi.reggie.reggie.service.DishService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R<PageInfo<Map<String,Object>>> findAllDishes(@RequestParam(value = "page") int page,
                                       @RequestParam(value = "pageSize") int pageSize,
                                       @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> dishes = dishService.getAllDish(name);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(dishes);
        return R.success(pageInfo);
    }

    @RequestMapping(method = RequestMethod.POST)
    public R<String> addDish(@RequestBody DishVo dishVo) {
        log.info("addDish : {}", dishVo.toString());
        List<DishFlavor> flavors = dishVo.getFlavors();
        dishService.addDish(dishVo);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateDish(@RequestBody DishVo dishVo) {
        dishService.updateDish(dishVo);
        return R.success();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<DishVo> getDishById(@PathVariable(value = "id") Long id) {
        log.info("getDishById : {}", id);
        DishVo dishVo = dishService.getDishById(id);
        return R.success(dishVo);
    }

    @RequestMapping(value = "/status/{status}", method = RequestMethod.POST)
    public R<String> stopOrStartDish(@RequestParam(value = "ids") String ids, @PathVariable(value = "status") Integer status){
        dishService.changeStatusById(status,ids);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public R<String> deleteDishById(@RequestParam(value = "ids") String ids) {
        log.info("deleteDishById : {}", ids);
        dishService.deleteDishById(ids);
        return R.success();
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<DishVo>> findDishByCategoryId(@RequestParam(value = "categoryId") Long categoryId) {
        List<DishVo> dishVoes = dishService.getDishByCategoryId(categoryId);
        return R.success(dishVoes);
    }

}
